layui.use(['table','layer'], function () {
    var table = layui.table;
    var layer = layui.layer;
    //表格渲染
    table.render({
        elem: '#test'
        , url: 'jay/Customer/showCustomers' //数据接口
        , parseData: function (res) { //res 即为原始返回的数据
            return {
                "code": 0, //解析接口状态
                "msg": "", //解析提示文本
                "count": res.obj.total, //解析数据长度
                "data": res.obj.list //解析数据列表，解析返回数据里面的数组对象
            }
        }
        , toolbar: '#bar'
        , defaultToolBar: ['default', 'print', 'export']
        , cols: [[ //表头
            { field: 'id', title: '序号', align: 'center', width: 80, sort: true, fixed: 'left' }
            , { field: 'cusName', title: '客户真实姓名', align: 'center', width: 140 }
            , { field: 'address', title: '地址', align: 'center', width: 300 }
            , { field: 'phone', title: '电话号码', align: 'center', width: 160 }
            , { fixed: '', title: '操作', align: 'center', toolbar: '#barDemo' }
        ]],
        id: 'testReload',
        limit: 10,
        even: true,
        event: true,
        page: true,
        done: function (res, curr, count) {
            $("#test").css("width", "100%");
        },
    });
    var $ = layui.$, active = {
        reload: function () {
            var cusName = $('#cusName').val();
            table.reload('testReload', {
                page: {
                    curr: 1//默认从第一页开始加载
                },
                where: {
                    cusName: cusName
                }
            });
        }
    }
    $('#find').on('click', function () {
        var type = 'reload';
        active[type] ? active[type].call(this) : '';
    });
    table.on('tool(test)', function (obj) {
        var data = obj.data;
        if (obj.event == 'upd') {
            layer.open({
                type: 2,
                title: '修改客户信息',
                area: ['600px', '380px'],
                content: 'updCustomer.html',
                maxmin: true,
                shadeClose: true,
                btn: ['取消'],
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    body.contents().find('#cusName').val(data.cusName);
                    body.contents().find('#address').val(data.address);
                    body.contents().find('#phone').val(data.phone);
                    body.contents().find('#cId').val(data.id);
                }
            })
        } else if (obj.event == 'del') {
            layer.confirm("确认要删除吗?", { btn: ["确认", "取消"] },
                function () {
                    $.ajax({
                        url: 'jay/Customer/deleteCustomer',
                        data: 'id=' + data.id,
                        success: function (res) {
                            if (res.obj != null) {
                                layer.msg("删除成功", { icon: 1 });
                                table.reload('testReload', {
                                    page: {
                                        curr: 1
                                    }
                                }
                                );
                            } else {
                                layer.msg("删除失败，请稍后再试!", { icon: 2 });
                            }
                        }
                    });
                }, function () {
                    layer.msg("您取消了操作", { icon: 2 });
                })
        }
    })
});
function addCustomer() {
    layui.use('layer', function () {
        var layer = layui.layer;

        layer.open({
            type: 2,
            area: ['600px', '380px'],
            title: '添加客户',
            content: 'addCustomer.html'
        });
    });
}